package com.nanyang.tms.manager.forecast.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.company.CompanyDao;
import com.nanyang.tms.dao.company.CompanyStoredDao;
import com.nanyang.tms.dao.company.CompanyTransactionRecordDao;
import com.nanyang.tms.domain.company.CompanyStoredVO;
import com.nanyang.tms.domain.company.CompanyTransactionRecordDO;
import com.nanyang.tms.domain.forecast.context.CalculateFreightContext;
import com.nanyang.tms.domain.forecast.context.saveforecast.SaveForecastContext;
@Component
public class TransactionRecordHandler extends AbstractTransHandler{
	@Autowired
	private CompanyStoredDao companyStoredDao;
	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private CompanyTransactionRecordDao companyTransactionRecordDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		CalculateFreightContext calculateFreightContext = (CalculateFreightContext) context
				.getInnerContext(CalculateFreightContext.class);
		SaveForecastContext saveForecastContext=(SaveForecastContext)context.getInnerContext(SaveForecastContext.class);
		try {
			CompanyStoredVO companyStored = companyStoredDao.getCompanyStored(calculateFreightContext.getCompanyCode());
			
			CompanyTransactionRecordDO record=  buildTransactionRecord(calculateFreightContext, saveForecastContext, companyStored);
			companyTransactionRecordDao.insert(record);
			return true;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		

	}
	
	private CompanyTransactionRecordDO buildTransactionRecord(CalculateFreightContext calculateFreightContext,SaveForecastContext saveForecastContext,CompanyStoredVO companyStored ){
		String recordSeq = TmsSerializeConstant.TRANSACTION_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.TRANSACTION_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
		CompanyTransactionRecordDO record=	CompanyTransactionRecordDO.builder()
		.actualBalance(companyStored.getActualBalance())
		.amount(calculateFreightContext.getAmount())
		.amountType(1)
		.billCode(saveForecastContext.getForecastCode())
		.billTime(new Date())
		.billTypeCode("FROZEN")
		.billTypeName("冻结")
		.cardName(companyStored.getCardName())
		.cardNo(companyStored.getCardNo())
		.companyCode(calculateFreightContext.getCompanyCode())
		.companyName(companyDao.selectByCode(calculateFreightContext.getCompanyCode()).getCompanyName())
		.frozenAmount(companyStored.getFrozenAmount())
		.pin(companyStored.getPin())
		.recordCode(recordSeq)
		.useBalance(companyStored.getUseBalance())
		.userName(companyStored.getUserName())
		.build();
		record.setIp(saveForecastContext.getIp());
		record.setGmtCreatePin(saveForecastContext.getPin());
		record.setGmtModifiedPin(saveForecastContext.getPin());
		return record;
	}

}
